package com.example.myxmpp.provider;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.net.Uri;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.myxmpp.dbhelper.SmsOpenHelper;

public class SmsProvider extends ContentProvider {


    private  static final  String AUTHORITIES = SmsProvider.class.getCanonicalName();

    static UriMatcher mUriMatcher;
    public static Uri URI_SMS = Uri.parse("content://"+AUTHORITIES+"/sms");
    public static  Uri URI_SESSION =Uri.parse("content://"+AUTHORITIES+"/session") ;

    private static final int SMS = 1;
    private static final int SESSION = 2;

    static {
        mUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        //添加匹配规则
        mUriMatcher.addURI(AUTHORITIES,"/sms",SMS);
        mUriMatcher.addURI(AUTHORITIES,"/session",SESSION);
    }

    private SmsOpenHelper helper;


    @Override
    public boolean onCreate() {
        //创建表
        helper = new SmsOpenHelper(getContext());
        if(helper != null){
            return  true;
        }
        return false;
    }

    @Nullable
    @Override
    public Cursor query(@NonNull Uri uri, @Nullable String[] projection, @Nullable String selection, @Nullable String[] selectionArgs, @Nullable String sortOrder) {
        Cursor cursor = null;
        switch (mUriMatcher.match(uri)){
            case SMS:
                cursor = helper.getReadableDatabase().query(SmsOpenHelper.T_SMS,projection,selection,selectionArgs,null,null,sortOrder);
                Log.i("query","+++++++++SmsProvider querySuccess+++++++");
                break;

            case SESSION:
                cursor = helper.getReadableDatabase().rawQuery("SELECT * FROM "+"(SELECT * FROM t_sms WHERE from_account = ? or to_account = ? ORDER BY time ASC)"
                +" GROUP BY session_account",selectionArgs);

                break;
            default:
                break;
        }
        return cursor;
    }

    @Nullable
    @Override
    public String getType(@NonNull Uri uri) {
        return null;
    }

    @Nullable
    @Override
    public Uri insert(@NonNull Uri uri, @Nullable ContentValues values) {

        switch (mUriMatcher.match(uri)){
            case SMS:
              long id =  helper.getWritableDatabase().insert(SmsOpenHelper.T_SMS,"",values);
                if (id >0){
                    Log.i("insert","+++++++++SmsProvider insertSuccess+++++++");
                    uri = ContentUris.withAppendedId(uri,id);
                    //发送数据改变的信号
                    getContext().getContentResolver().notifyChange(SmsProvider.URI_SMS,null);
                }
              break;

            default:
                break;
        }
        return uri;
    }

    @Override
        public int delete(@NonNull Uri uri, @Nullable String selection, @Nullable String[] selectionArgs) {
         int deleteCount =0;
        switch (mUriMatcher.match(uri)){
            case SMS:
                deleteCount = helper.getWritableDatabase().delete(SmsOpenHelper.T_SMS,selection,selectionArgs);
                if (deleteCount >0){
                    Log.i("delete","+++++++++SmsProvider deleteSuccess+++++++");
                    //发送数据改变的信号
                    getContext().getContentResolver().notifyChange(SmsProvider.URI_SMS,null);
                }
                break;

            default:
                break;
        }
        return deleteCount;
    }

    @Override
    public int update(@NonNull Uri uri, @Nullable ContentValues values, @Nullable String selection, @Nullable String[] selectionArgs) {
        int updateCount =0;
        switch (mUriMatcher.match(uri)){
            case SMS:
                //更新了几条数据
               updateCount = helper.getWritableDatabase().update(SmsOpenHelper.T_SMS,values,selection,selectionArgs);
               if (updateCount >0){
                   Log.i("update","+++++++++SmsProvider updateSuccess+++++++");
                   //发送数据改变的信号
                   getContext().getContentResolver().notifyChange(SmsProvider.URI_SMS,null);
               }
                break;

            default:
                break;
        }
        return updateCount;
    }
}
